  const API_BASE_URL = 'https://toptalents.top/tuopu'
const wxRpx = function() {
	let rpx;
	wx.getSystemInfo({
		success: (res) => {
			rpx = res.screenWidth / 750
		},
		fail: (res) => {
			console.log(res, "fail getsysteminfo")
		}
	})
	return rpx;
}

/**
 * @param {Object} url 上传图片的服务器地址
 * @param {Object} urlName  FormData的name
 * @param {Object} show   显示图片的地址放在this.data的哪个变量,可以为对象属性如userinfo.img
 * @param {Object} num  选择图片数量
 * @param {Object} callback  上传api的回调res.data
 */
/* app.util.wxUploadImage("/upload_file", "file", "bb.cc.dd",2,
		(res,status) => {
			console.log(res, status,"iii")
		}), */
const wxUploadImage = function(url, urlName, show, num, callback) {
	if (!/http/.test(url)) url = API_BASE_URL + url;
	return function() {
		var t = this;
		wx.chooseImage({
			count: num,
			sizeType: ["original", "compressed"],
			sourceType: ["album", "camera"],
			success: function(e) {
				var arr = e.tempFilePaths;
				t.setData({
					[show]: num == 1 ? e.tempFilePaths[0] : e.tempFilePaths
				})
				let i = 0,
					l = arr.length;
				for (var key in arr) wx.uploadFile({
					url: url,
					filePath: arr[key],
					name: urlName,
					success: function(res) {
						/*						上传后才显示图片,把上边注释掉,下边解注释
						 						if (++i==l) {
													t.setData({
														[show]: num == 1 ? e.tempFilePaths[0] : e.tempFilePaths
													})
												} */
						callback(JSON.parse(res.data), ++i == l);
					}
				});
			}
		});
	}
}

const wxNaviback = function(url) {
	let arr = getCurrentPages(),
		delta;
	for (let k in arr) {
		if (arr[k].route == url) {
			delta = arr.length - 1 - k;
			break;
		}
	}
	if (delta) {
		wx.navigateBack({
			delta: delta
		});
	} else {
		wx.reLaunch({
			url: url
		})
	}
}

const wxSaveImage = function(key) {
	return function() {
		var t = this;
		wx.getSetting({
			success: res => {
				let str = res.authSetting['scope.writePhotosAlbum'];
				if (str || typeof str == "undefined") {
					wx.showLoading({
						title: '保存中...',
						mask: true,
					});
					wx.downloadFile({
						url: t.data[key],
						success: function(res) {
							if (res.statusCode === 200) {
								let img = res.tempFilePath;
								wx.saveImageToPhotosAlbum({
									filePath: img,
									success(res) {
										wx.showToast({
											title: '保存成功',
											icon: 'success',
											duration: 2000
										});
									},
									fail(err) {
										let msg = '保存失败';
										console.log(err, /cancel/.test(err.errMsg), "zzzzzzzzzzzz")
										if (/cancel/.test(err.errMsg)) {
											msg = '取消保存'
										}
										wx.showToast({
											title: msg,
											icon: 'none',
											duration: 2000
										});
									}
								});
							}
						},
						complete: function() {
							wx.hideLoading();
						}
					});
				} else if (str === false) {
					wx.showModal({
						title: "需要授权才能保持图片",
						showCancel: false,
						success: function() {
							wx.openSetting({
								success(res) {
									if (res.authSetting['scope.writePhotosAlbum']) {
										console.log("ok")
									} else {
										wx.showToast({
											title: '您没有授权，无法保存到相册',
											icon: 'none'
										})
									}
								}
							})

						}
					})
				}
			}
		})
	}

}

const wxPhone = function(e) {
	return function() {
		wx.makePhoneCall({
			phoneNumber: e * 1,
			fail(err) {
				wx.showToast({
					title: err,
					icon: "none"
				})
			}
		})
	}
}
export default {
	wxRpx,
	wxUploadImage,
	wxNaviback,
	wxSaveImage,
	wxPhone
}